<template>
  <div class="login">
    <div class="form">
      <div class="title">
        智慧点餐平台
      </div>
      <el-form :model="formData" :rules="rules" ref="formRef">
        <el-form-item>
          <el-input v-model="formData.account" placeholder="请输入账号" >
           <template #prefix>
            <el-icon class="el-input__icon">
               <UserFilled  color="#fff"/>
            </el-icon>
          </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="formData.password" type="password"
           show-password placeholder="请输入密码">
            <template #prefix>
              <el-icon class="el-input__icon">
                <Lock  color="#fff"/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="login">立即登录</el-button>
        </el-form-item>
      </el-form>
    
    </div>
  </div>
</template>
<script setup>
import { reactive } from 'vue'
import {useRouter} from 'vue-router'
import {ref} from 'vue'
// import { UserFilled,Lock} from '@element-plus/icons-vue'
import { ElForm, ElFormItem, ElInput } from 'element-plus';
import  {checkLogin} from '@/api/account'


import { ElMessage } from 'element-plus'
const router = useRouter()

const formData = reactive({
  account: '',
  password: ''
})
const formRef= ref(null)
const validateAccount = (rule, value, callback) => {
  let reg = /^([a-z]|[0-9]){4,11}$/
  if (reg.test(value)) {
    callback()
  } else {
    callback(new Error('请输入正确的账号'))
  }

}

const validatePassword = (rule, value, callback) => {
  let reg = /^([a-z]|[0-9]){4,11}$/
  if (reg.test(value)) {
    callback()
  } else {
    callback(new Error('请输入正确的密码'))
  }
}

const rules = reactive({
  account: [
    { validator: validateAccount , trigger: 'blur' }
  ],
  password:[
    {validator: validatePassword, trigger: 'blur' }
  ]
})
const login = () => {
  formRef.value.validate(async (res) => {
    if (res) {
      let { code, msg, id, role, token } = await checkLogin(formData);
      if (code == 0) {
        sessionStorage.setItem('user', JSON.stringify({ id, role, token }))
        ElMessage.success(msg)
        router.push('/home')
      } else {
        ElMessage.error(msg)
      }
    }
  })
}
</script>

<style scoped>
.login {
  width: 100%;
  height: 100%;
  background-color: #2d3a4b;
  text-align: center;
}

.form {
  width: 400px;
  height: 300px;
  
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.el-input{
  margin: 10px 50px;
  width: 300px;
  height: 30px;
}

.title {
  width: 100%;
  height: 50px;
  color: #fff;
  font-size: 20px;
  text-align: center;
  line-height: 50px
}
:deep(.el-input__wrapper){
  background-color:  transparent;

}
:deep(.el-input__inner){
  color: #fff;
}
</style>